﻿using HarmonyLib;
using Klei.AI;
using Klei.AI.DiseaseGrowthRules;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using UnityEngine;

namespace oxygen_mod
{
    public partial class Patches
    {
        /// <summary>
        /// 氧气扩散器
        /// </summary>
        [HarmonyPatch(typeof(MineralDeoxidizerConfig), "CreateBuildingDef")]
        public class Patch_A1
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 10f;
            }
        }

        /// <summary>
        /// 液泵
        /// </summary>
        [HarmonyPatch(typeof(LiquidPumpConfig), "CreateBuildingDef")]
        public class Patch_A2
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 碎石机
        /// </summary>
        [HarmonyPatch(typeof(RockCrusherConfig), "CreateBuildingDef")]
        public class Patch_A3
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 40f;
                __result.ExhaustKilowattsWhenActive = 0f;
            }
        }

        /// <summary>
        /// 气泵
        /// </summary>
        [HarmonyPatch(typeof(GasPumpConfig), "CreateBuildingDef")]
        public class Patch_A4
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 点唱机器人
        /// </summary>
        [HarmonyPatch(typeof(PhonoboxConfig), "CreateBuildingDef")]
        public class Patch_A5
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 80f;
            }
        }

        /// <summary>
        /// 按摩床
        /// </summary>
        [HarmonyPatch(typeof(MassageTableConfig), "CreateBuildingDef")]
        public class Patch_A6
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 40f;
            }
        }

        /// <summary>
        /// 冰箱
        /// </summary>
        [HarmonyPatch(typeof(RefrigeratorConfig), "CreateBuildingDef")]
        public class Patch_A7
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 电解器
        /// </summary>
        [HarmonyPatch(typeof(ElectrolyzerConfig), "CreateBuildingDef")]
        public class Patch_A8
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 人力发电机
        /// </summary>
        [HarmonyPatch(typeof(ManualGeneratorConfig), "CreateBuildingDef")]
        public class Patch_A9
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.GeneratorWattageRating = 800f;
            }
        }

        /// <summary>
        /// 孵化器
        /// </summary>
        [HarmonyPatch(typeof(CookingStationConfig), "CreateBuildingDef")]
        public class Patch_A10
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 电动烤炉
        /// </summary>
        [HarmonyPatch(typeof(EggIncubatorConfig), "CreateBuildingDef")]
        public class Patch_A11
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 电动烤炉
        /// </summary>
        [HarmonyPatch(typeof(MicrobeMusherConfig), "CreateBuildingDef")]
        public class Patch_A12
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 工作台
        /// </summary>
        [HarmonyPatch(typeof(CraftingTableConfig), "CreateBuildingDef")]
        public class Patch_A13
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 研究站
        /// </summary>
        [HarmonyPatch(typeof(ResearchCenterConfig), "CreateBuildingDef")]
        public class Patch_A14
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
            }
        }

        /// <summary>
        /// 超级计算机
        /// </summary>
        [HarmonyPatch(typeof(AdvancedResearchCenterConfig), "CreateBuildingDef")]
        public class Patch_A15
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 40f;
            }
        }

        /// <summary>
        /// 净水器
        /// </summary>
        [HarmonyPatch(typeof(WaterPurifierConfig), "CreateBuildingDef")]
        public class Patch_A16
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 40f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 配药桌
        /// </summary>
        [HarmonyPatch(typeof(ApothecaryConfig), "CreateBuildingDef")]
        public class Patch_A17
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 机械气闸
        /// </summary>
        [HarmonyPatch(typeof(PressureDoorConfig), "CreateBuildingDef")]
        public class Patch_A18
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 20f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 气体过滤器
        /// </summary>
        [HarmonyPatch(typeof(GasFilterConfig), "CreateBuildingDef")]
        public class Patch_A19
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 10f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 氢气发电机
        /// </summary>
        [HarmonyPatch(typeof(HydrogenGeneratorConfig), "CreateBuildingDef")]
        public class Patch_A20
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.GeneratorWattageRating = 2400f;
            }
        }

        /// <summary>
        /// 材料研究终端
        /// </summary>
        [HarmonyPatch(typeof(NuclearResearchCenterConfig), "CreateBuildingDef")]
        public class Patch_A21
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 80f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 温度调节器
        /// </summary>
        [HarmonyPatch(typeof(AirConditionerConfig), "CreateBuildingDef")]
        public class Patch_A22
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 40f;
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 消防滑杆
        /// </summary>
        [HarmonyPatch(typeof(FirePoleConfig), "CreateBuildingDef")]
        public class Patch_A23
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.ConstructionTime = 2f;
            }
        }

        /// <summary>
        /// 巨型电池
        /// </summary>
        [HarmonyPatch(typeof(BatteryMediumConfig), "CreateBuildingDef")]
        public class Patch_A24
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.ConstructionTime = 10f;
            }
        }

        /// <summary>
        /// 巨型电池
        /// </summary>
        [HarmonyPatch(typeof(InsulationTileConfig), "CreateBuildingDef")]
        public class Patch_A25
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.ConstructionTime = 5f;
            }
        }

        /// <summary>
        /// 只能存储箱
        /// </summary>
        [HarmonyPatch(typeof(StorageLockerSmartConfig), "CreateBuildingDef")]
        public class Patch_A26
        {
            public static void Postfix(ref BuildingDef __result)
            {
                __result.EnergyConsumptionWhenActive = 10f;
                __result.ConstructionTime = 10f;
            }
        }

        // ================================ ================================

        /// <summary>
        /// 碎石机
        /// </summary>
        [HarmonyPatch(typeof(RockCrusherConfig), "ConfigureBuildingTemplate")]
        public static class Patch_B1
        {
            private static void Postfix(GameObject go, Tag prefab_tag)
            {
                go.AddOrGet<ComplexFabricator>().duplicantOperated = false;
            }
        }

        /// <summary>
        /// 金属精炼器
        /// </summary>
        [HarmonyPatch(typeof(MetalRefineryConfig), "ConfigureBuildingTemplate")]
        public static class Patch_B2
        {
            private static void Postfix(GameObject go, Tag prefab_tag)
            {
                go.AddOrGet<ComplexFabricator>().duplicantOperated = false;
            }
        }

        /// <summary>
        /// 储气库
        /// </summary>
        [HarmonyPatch(typeof(GasReservoirConfig), "ConfigureBuildingTemplate")]
        public static class Patch_B3
        {
            public static void Postfix(GasReservoirConfig __instance, ref GameObject go)
            {
                Storage storage = BuildingTemplates.CreateDefaultStorage(go, false);
                storage.capacityKg = 100000f;
                storage.SetDefaultStoredItemModifiers(Storage.StandardInsulatedStorage);
                storage.allowUIItemRemoval = true;
                ConduitConsumer conduitConsumer = go.AddOrGet<ConduitConsumer>();
                conduitConsumer.capacityKG = storage.capacityKg;
            }
        }

        /// <summary>
        /// 储液库
        /// </summary>
        [HarmonyPatch(typeof(LiquidReservoirConfig), "ConfigureBuildingTemplate")]
        public static class Patch_B4
        {
            public static void Postfix(LiquidReservoirConfig __instance, ref GameObject go)
            {
                Storage storage = BuildingTemplates.CreateDefaultStorage(go, false);
                storage.capacityKg = 100000f;
                storage.SetDefaultStoredItemModifiers(Storage.StandardInsulatedStorage);
                storage.allowUIItemRemoval = true;
                ConduitConsumer conduitConsumer = go.AddOrGet<ConduitConsumer>();
                conduitConsumer.capacityKG = storage.capacityKg;
            }
        }

        /// <summary>
        /// 电解器
        /// </summary>
        [HarmonyPatch(typeof(ElectrolyzerConfig), "ConfigureBuildingTemplate")]
        public class Patch_B5
        {
            public static void Postfix(GameObject go, Tag prefab_tag)
            {
                CellOffset cellOffset = new CellOffset(0, 1);
                Electrolyzer electrolyzer = go.AddOrGet<Electrolyzer>();
                electrolyzer.emissionOffset = cellOffset;
                go.AddOrGet<ElementConverter>().outputElements = new ElementConverter.OutputElement[]
                {
                    new ElementConverter.OutputElement(2f, SimHashes.Oxygen, 343.15f, false, false, cellOffset.x, cellOffset.y, 1f, byte.MaxValue, 0, true),
                    new ElementConverter.OutputElement(1f, SimHashes.Hydrogen, 343.15f, false, false, cellOffset.x, cellOffset.y, 1f, byte.MaxValue, 0, true)
                };
            }
        }

        /// <summary>
        /// 梯子
        /// </summary>
        [HarmonyPatch(typeof(LadderConfig), "ConfigureBuildingTemplate")]
        public class Patch_B6
        {
            public static void Postfix(GameObject go, Tag prefab_tag)
            {
                Ladder ladder = go.AddOrGet<Ladder>();
                ladder.upwardsMovementSpeedMultiplier = 4f;
                ladder.downwardsMovementSpeedMultiplier = 4f;
            }
        }

        /// <summary>
        /// 氧气扩散器
        /// </summary>
        [HarmonyPatch(typeof(MineralDeoxidizerConfig), "ConfigureBuildingTemplate")]
        public class Patch_B7
        {
            public static void Postfix(GameObject go, Tag prefab_tag)
            {
                CellOffset cellOffset = new CellOffset(0, 1);
                Electrolyzer electrolyzer = go.AddOrGet<Electrolyzer>();
                electrolyzer.emissionOffset = cellOffset;
                ElementConverter elementConverter = go.AddOrGet<ElementConverter>();
                elementConverter.consumedElements = new ElementConverter.ConsumedElement[]
                {
                    new ElementConverter.ConsumedElement(new Tag("Algae"), 0.2f, true)
                };
                elementConverter.outputElements = new ElementConverter.OutputElement[]
                {
                    new ElementConverter.OutputElement(1f, SimHashes.Oxygen, 303.15f, false, false, cellOffset.x, cellOffset.y, 1f, byte.MaxValue, 0, true)
                };
            }
        }


        /// <summary>
        /// 消防滑杆
        /// </summary>
        [HarmonyPatch(typeof(FirePoleConfig), "ConfigureBuildingTemplate")]
        public class Patch_B8
        {
            public static void Postfix(GameObject go, Tag prefab_tag)
            {
                Ladder ladder = go.AddOrGet<Ladder>();
                ladder.upwardsMovementSpeedMultiplier = 4f;
                ladder.downwardsMovementSpeedMultiplier = 4f;
            }
        }

        // ================================ ================================


        /// <summary>
        /// 冰箱
        /// </summary>
        [HarmonyPatch(typeof(RefrigeratorConfig), "DoPostConfigureComplete")]
        public class Patch_C1
        {
            public static void Postfix(GameObject go)
            {
                Storage storage = go.AddOrGet<Storage>();
                storage.capacityKg = 20000f;
            }
        }


        /// <summary>
        /// 存储库
        /// </summary>
        [HarmonyPatch(typeof(StorageLockerConfig), "DoPostConfigureComplete")]
        public class Patch_C2
        {
            public static void Postfix(GameObject go)
            {
                Storage storage = go.AddOrGet<Storage>();
                storage.capacityKg = 800000f;
            }
        }

        /// <summary>
        /// 巨型电池
        /// </summary>
        [HarmonyPatch(typeof(BatteryMediumConfig), "DoPostConfigureComplete")]
        public class Patch_C3
        {
            public static void Postfix(GameObject go)
            {
                Battery battery = go.AddOrGet<Battery>();
                battery.capacity = 5000000f; // 5000 千焦
                battery.joulesLostPerSecond = 0f; // 每秒的损耗
            }
        }

        /// <summary>
        /// 只能存储库
        /// </summary>
        [HarmonyPatch(typeof(StorageLockerSmartConfig), "DoPostConfigureComplete")]
        public class Patch_C4
        {
            public static void Postfix(GameObject go)
            {
                Storage storage = go.AddOrGet<Storage>();
                storage.capacityKg = 50000f;
            }
        }

        // ================================ ================================

        /// <summary>
        /// 电线功率
        /// </summary>
        [HarmonyPatch(typeof(Wire), "GetMaxWattageAsFloat")]
        public class Patch_D1
        {
            public static void Postfix(Wire __instance, ref float __result, Wire.WattageRating rating)
            {
                switch (rating)
                {
                    case Wire.WattageRating.Max500:
                        __result = 500f;
                        break;
                    case Wire.WattageRating.Max1000:
                        __result = 8 * 1000f;
                        break;
                    case Wire.WattageRating.Max2000:
                        __result = 16 * 1000f;
                        break;
                    case Wire.WattageRating.Max20000:
                        __result = 80 * 1000f;
                        break;
                    case Wire.WattageRating.Max50000:
                        __result = 200 * 1000f;
                        break;
                    default:
                        __result = 0f;
                        break;
                }
            }
        }

        /// <summary>
        /// 净水无细菌
        /// </summary>
        [HarmonyPatch(typeof(FoodGerms), "PopulateElemGrowthInfo")]
        public class Patch_D2
        {
            public static void Postfix(FoodGerms __instance)
            {
                __instance.growthRules.Add(new ElementGrowthRule(SimHashes.Water)
                {
                    populationHalfLife = new float?(10f),
                    overPopulationHalfLife = new float?(10f),
                    minDiffusionCount = new int?(1000000)
                });
            }
        }

        [HarmonyPatch(typeof(WorldDamage), "OnDigComplete")]
        public class Patch_D3
        {
            private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instr)
            {
                IEnumerable<CodeInstruction> source = instr;
                foreach (CodeInstruction codeInstruction in source.ToList<CodeInstruction>())
                {
                    if ((OpCode)codeInstruction.opcode == OpCodes.Ldc_R4 && (float)codeInstruction.operand == 0.5)
                    {
                        codeInstruction.operand = 1f;
                    }
                    yield return codeInstruction;
                }
            }
        }
    }
}
